﻿
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Security.Cryptography
Imports System.Text


Public Class Md5
    Function getMd5Hash(ByVal input As String) As String
        ' 创建新的一个MD5CryptoServiceProvider对象的实例。
        Dim md5Hasher As New MD5CryptoServiceProvider()
        ' 输入的字符串转换为字节数组，并计算哈希。
        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(input))
        ' 创建一个新的StringBuilder收集的字节，并创建一个字符串。
        Dim sBuilder As New StringBuilder()
        ' 通过每个字节的哈希数据和格式为十六进制字符串的每一个循环。
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i
        ' 返回十六进制字符串。
        Return sBuilder.ToString()
    End Function
    ' 验证对一个字符串的哈希值。
    Function verifyMd5Hash(ByVal input As String, ByVal hash As String) As Boolean
        ' 哈希的输入。
        Dim hashOfInput As String = getMd5Hash(input)
        ' 创建StringComparer1的哈希进行比较。
        Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
        If 0 = comparer.Compare(hashOfInput, hash) Then
            Return True
        Else
            Return False
        End If
    End Function


    Public Sub writeLog(ByVal v As String)
        Dim keyinfo_path = System.IO.Directory.GetCurrentDirectory() & "\log.txt"
        Dim file As New System.IO.StreamWriter(keyinfo_path, True)
        file.WriteLine(v)
        file.Close()
    End Sub

    Public Function readFile(ByVal path As String, Optional ByVal encode As Boolean = False) As String
        Dim encoding As Text.Encoding = IIf(encode, System.Text.UTF8Encoding.Default, System.Text.ASCIIEncoding.Default)
        Dim RT As StreamReader
        RT = New StreamReader(path, encoding)
        Dim ret As String = RT.ReadToEnd() '读取全部  
        RT.Close()
        Return ret
    End Function

    Public Shared Function EncryptDes(ByVal SourceStr As String, Optional ByVal myKey As String = "K8i$m152", Optional ByVal myIV As String = "8956kopl") As String '使用的DES对称加密
        Dim des As New System.Security.Cryptography.DESCryptoServiceProvider 'DES算法
        'Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider'TripleDES算法
        Dim inputByteArray As Byte()
        inputByteArray = System.Text.Encoding.Default.GetBytes(SourceStr)
        des.Key = System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8个字符，TripleDES要24个字符
        des.IV = System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8个字符，TripleDES要24个字符
        Dim ms As New System.IO.MemoryStream
        Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
        Dim sw As New System.IO.StreamWriter(cs)
        sw.Write(SourceStr)
        sw.Flush()
        cs.FlushFinalBlock()
        ms.Flush()
        Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
    End Function

    Public Shared Function DecryptDes(ByVal SourceStr As String, Optional ByVal myKey As String = "K8i$m152", Optional ByVal myIV As String = "8956kopl") As String    '使用标准DES对称解密
        If String.IsNullOrEmpty(SourceStr) Then
            Return SourceStr
        End If
        If SourceStr = "" Then
            Return SourceStr
        End If
        Dim des As New System.Security.Cryptography.DESCryptoServiceProvider 'DES算法
        'Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider'TripleDES算法
        des.Key = System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8个字符，TripleDES要24个字符
        des.IV = System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8个字符，TripleDES要24个字符
        Dim buffer As Byte() = Convert.FromBase64String(SourceStr)
        Dim ms As New System.IO.MemoryStream(buffer)
        Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read)
        Dim sr As New System.IO.StreamReader(cs)
        DecryptDes = sr.ReadToEnd()
        Return DecryptDes
    End Function

End Class
